package com.sharecode.stock.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.sharecode.stock.repository.StockHistoryRepository;
import com.sharecode.stock.strategy.StrategySuper;
import com.sharecode.stock.utils.DateUtils;
import com.sharecode.stock.vo.StockDateConfig;
import com.sharecode.stock.vo.stock.StockCode;
import com.sharecode.stock.vo.stock.StockHistory;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class StockHistoryService {

	@Autowired
	private StockHistoryRepository stockHistoryRepository;
	@Autowired
	private StockDateConfigService stockDateConfigService;
	@Autowired
	private StockCodeService stockCodeService;

	public void save(StockHistory stockHistory) {
		stockHistoryRepository.save(stockHistory);
	}

	public void save(List<StockHistory> stockHistories) {
		stockHistoryRepository.saveAll(stockHistories);
	}

	public List<StockHistory> findByCodeAndCycle(String code,int cycle) {
		return stockHistoryRepository.findByCodeAndCycleOrderByDate(code,cycle);
	}

	public void handleHisory() {
		List<StockCode> codes = stockCodeService.getAll();
		List<StockDateConfig> configs = stockDateConfigService.findAll();
		long millis = System.currentTimeMillis();
		for (StockCode stockCode : codes) {
			List<StockHistory> stockHistories = new ArrayList<>();
			for (StockDateConfig config : configs) {
				String code = stockCode.getCode();
				List<StockHistory> histories = stockHistoryRepository.findByParam(code, config.getStartDate(),
						config.getEndDate());
				if (CollectionUtils.isNotEmpty(histories)) {
					log.info("histories->{},{},{},{}", code, config.getStartDate(), config.getEndDate(),
							histories.size());
					StockHistory history = new StockHistory();

					int size = histories.size();
					for (int i = 0; i < size; i++) {
						StockHistory h = histories.get(i);
						if (i == 0) {
							history = JSONObject.parseObject(JSONObject.toJSONString(h), StockHistory.class);
							history.setId(null);
						} else if (i == size - 1) {
							history.setClose(h.getClose());
						} else {
							history.setMarket(bigAdd(history.getMarket(), h.getMarket()));
							history.setVolume(bigAdd(history.getVolume(), h.getVolume()));
							history.setHigh(StrategySuper.max(history.getHigh(), h.getHigh()));
							history.setLow(StrategySuper.min(history.getLow(), h.getLow()));
						}
					}
					history.setCycle(config.getCycle());
					stockHistories.add(history);
				}
				if (DateUtils.parse(config.getEndDate(), DateUtils.FORMAT_YYYY_MM_DD).getTime() > millis) {
					break;
				}
			}
			
			save(stockHistories);
		}
	}

	private BigDecimal bigAdd(BigDecimal b1, BigDecimal b2) {
		if (b1 == null) {
			b1 = new BigDecimal("0");
		}
		if (b2 == null) {
			b2 = new BigDecimal("0");
		}
		return b1.add(b2);
	}

}
